﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using GContacts = Google.GData.Contacts;
using GClient = Google.GData.Client;
using GExtensions = Google.GData.Extensions;
using System.Collections.ObjectModel;
using System.Reflection;

namespace GSync
{

    public class GoogleContactsProvider : 
        GoogleDataProvider<GContacts.ContactEntry, GContacts.ContactsFeed, GContacts.ContactsService>
    {

        protected override Google.GData.Contacts.ContactsFeed Feed
        {
            get 
            {
                if( _Feed == null )
                {
                    GContacts.ContactsQuery _SelectQuery = new GContacts.ContactsQuery( FeedUri );
                    _SelectQuery.UseSSL = true;
                    //_SelectQuery.MaxPublication = DateTime.Now.AddDays(365);
                    //_SelectQuery.MinPublication = DateTime.Now.AddDays(-2000);
                    _SelectQuery.NumberToRetrieve = 200;
                  
                    _Feed = Service.Query(_SelectQuery);
                }
                return _Feed;
            }
        }

        protected override Google.GData.Contacts.ContactsService Service
        {
            get 
            {
                if (_Service == null)
                {
                    string appName = GSyncFactory.Instance.SettingsManager.AppVersion;

                    _Service = new Google.GData.Contacts.ContactsService(appName);
                    _Service.setUserCredentials(GSyncFactory.Instance.SettingsManager.ServerAccountName,
                                                GSyncFactory.Instance.SettingsManager.ServerPassword);
                }

                return _Service;
            }
        }



        protected override string FeedUri
        {
            get { return GContacts.ContactsQuery.CreateContactsUri("default"); }
        }

        protected override void CopyToNative(InternalEntry entry, Google.GData.Contacts.ContactEntry nativeContact)
        {
            InternalContact contact = entry as InternalContact;


            nativeContact.Title.Text = contact.Name;
            nativeContact.Content.Content = contact.Notes;

            for (int i = 0; i < contact.EMails.Count; i++)
            {
                if (nativeContact.Emails.Count == i)
                {
                    nativeContact.Emails.Add(new Google.GData.Extensions.EMail(contact.EMails[i]));
                    nativeContact.Emails[i].Rel = Google.GData.Extensions.ContactsRelationships.IsHome;

                    if (i == 0)
                        nativeContact.Emails[0].Primary = true;
                }
                else
                    nativeContact.Emails[i].Address = contact.EMails[i];
            }


            for (int i = 0; i < contact.IMs.Count; i++)
            {
                if (nativeContact.IMs.Count == i)
                {
                    nativeContact.IMs.Add(new Google.GData.Extensions.IMAddress(contact.IMs[i]));
                    nativeContact.IMs[i].Rel = Google.GData.Extensions.ContactsRelationships.IsHome;

                    if (i == 0)
                        nativeContact.IMs[0].Primary = true;
                }
                else
                    nativeContact.IMs[i].Address = contact.IMs[i];
            }


            for (int i = 0; i < contact.PhoneNumbers.Count; i++)
            {
                if (nativeContact.Phonenumbers.Count == i)
                {
                    nativeContact.Phonenumbers.Add(new Google.GData.Extensions.PhoneNumber(contact.PhoneNumbers[i]));
                    nativeContact.Phonenumbers[i].Rel = Google.GData.Extensions.ContactsRelationships.IsHome;

                    if (i == 0)
                        nativeContact.Phonenumbers[0].Primary = true;
                }
                else
                    nativeContact.Phonenumbers[i].Value = contact.PhoneNumbers[i];
            }
        }

        protected override InternalEntry ConvertToInternal(Google.GData.Contacts.ContactEntry nativeEntry)
        {
            InternalContact contact = new InternalContact();
            contact.ID = nativeEntry.Id.AbsoluteUri.Substring(nativeEntry.Id.AbsoluteUri.LastIndexOf('/') + 1);

            var emails = from GExtensions.EMail email in nativeEntry.Emails select email.Address;
            if (emails.Count() > 0)
                contact.EMails.AddRange(emails);


            var ims = from GExtensions.IMAddress im in nativeEntry.IMs select im.Address;
            if (ims.Count() > 0)
                contact.IMs.AddRange(ims);


            var phones = from GExtensions.PhoneNumber phone in nativeEntry.Phonenumbers select phone.Value;
            if (phones.Count() > 0)
                contact.PhoneNumbers.AddRange(phones);


            contact.Name = nativeEntry.Title.Text;
            contact.Notes = nativeEntry.Content.Content;

            return contact;
        }



    }

}
